//
//  Moive_Data.m
//  豆瓣zlw
//
//  Created by lanou3g on 16/1/15.
//  Copyright © 2016年 zlw. All rights reserved.
//

#import "Moive_Data.h"
static Moive_Data *share=nil;
@implementation Moive_Data

+(Moive_Data *)shareMoiveData
{
    @synchronized(self)
    {
        if (share == nil)
        {
            share=[[Moive_Data alloc] init];
        }
        return share;
    }
}
//数据库对象
static sqlite3 *db=nil;
//******************************打开数据库
-(void)openMoiveData
{
    if (db == nil)
    {
        
        _filePath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"movie.sqlite"];
        NSLog(@"%@",_filePath);
        //打开数据库
        int result=sqlite3_open(_filePath.UTF8String, &db);
        if (result == SQLITE_OK)
        {
            NSLog(@"成功打开数据库");
            //创建一个一个表格
            NSString *sql=@"CREATE  TABLE  IF NOT EXISTS movieModel (movie INTEGER PRIMARY KEY  NOT NULL , plot_simple TEXT ,tile TEXT ,genres TEXT ,country TEXT ,runtime TEXT ,poster TEXT ,rating_count TEXT,rating TEXT , release_date TEXT ,actors TEXT)";
            //esec就是用来执行sql语句的
            int result1=sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
            if (result1 == SQLITE_OK)
            {
                NSLog(@"创建表格成功");
            }else
            {
                NSLog(@"创建表格失败");
            }
        }
    }else
    {
        return;
    }
}
//***************************关闭数据库*******************************
-(void)closeMoiveData
{
    if (db != nil)
    {
        int result=sqlite3_close(db);
        if (result == SQLITE_OK)
        {
            NSLog(@"关闭数据库成功");
            db=nil;
        }else
        {
            NSLog(@"关闭数据库失败");
        }
    }
    
}
//*****************往数据库的表格里面添加数据****************************
-(void)addMoiveData:(MovieDetaModel *)Mv
{
    //把闯进来的数据格式化放在sql与语句汇总
    NSString *sql=[NSString stringWithFormat:@"INSERT INTO movieModel(plot_simple , tile , genres , country , runtime , poster ,rating_count , rating , release_date ,actors) VALUES ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@')",Mv.plot_simple,Mv.tile,Mv.genres,Mv.country,Mv.runtime,Mv.poster,Mv.rating_count,Mv.rating,Mv.release_date,Mv.actors];
    //执行sql语句
    int result=sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK)
    {
        NSLog(@"添加数据成功");
    }else
    {
        NSLog(@"添加数据失败");
    
    }


    
}
//************************删除信息************************************
-(void)deleteMoiveData
{

    NSFileManager *manager=[NSFileManager defaultManager];
    [manager removeItemAtPath:_filePath    error:nil];
}
//**************************更新数据****************************
-(void)updataMoiveData:(MovieDetaModel *)Mv
{
    NSString *sql=[NSString stringWithFormat:@"UPDATE movieModel SET tile ='%@'",Mv.tile];
    int result=sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK)
    {
        NSLog(@"数据更新成功");
    }else
    {
        NSLog(@"数据更新失败");
    }
}
//**************************查找数据************************
-(NSMutableArray *)selectAllMoive
{
    NSMutableArray *array=[NSMutableArray array];
    NSString *sql=@"SELECT * FROM movieModel";
    sqlite3_stmt *stmt=nil;
    int result=sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK)
    {
        NSLog(@"开始查找数据");
        while (sqlite3_step(stmt)==SQLITE_ROW)
        {
            //开始查找plot_simple
            char *plot_simple1=(char *)sqlite3_column_text(stmt, 1);
            NSString *plot_simple=[NSString stringWithCString:plot_simple1 encoding:NSUTF8StringEncoding];
            //开始查找tilel
            char *tile1=(char *)sqlite3_column_text(stmt, 2);
            NSString *tile=[NSString stringWithCString:tile1 encoding:NSUTF8StringEncoding];
            //开始查找genres
            char *genrs1=(char *)sqlite3_column_text(stmt, 3);
            NSString *geners=[NSString stringWithCString:genrs1 encoding:NSUTF8StringEncoding];
            //开始查找country
            char *country1=(char *)sqlite3_column_text(stmt, 4);
            NSString *country=[NSString stringWithCString:country1 encoding:NSUTF8StringEncoding];
            //开始查找runtime
            char *runtime1=(char *)sqlite3_column_text(stmt, 5);
            NSString *runtime=[NSString stringWithCString:runtime1 encoding:NSUTF8StringEncoding];
            //开始查找poster
            char *poster1=(char *)sqlite3_column_text(stmt, 6);
            NSString *poster=[NSString stringWithCString:poster1 encoding:NSUTF8StringEncoding];
            //开始查找rating_count
            char *rating_count1=(char *)sqlite3_column_text(stmt, 7);
            NSString *rating_count=[NSString stringWithCString:rating_count1 encoding:NSUTF8StringEncoding];
            //开始查找rating
            char *rating1=(char *)sqlite3_column_text(stmt, 8);
            NSString *rating=[NSString stringWithCString:rating1 encoding:NSUTF8StringEncoding];
            //开始查找release_data
            char *release_data1=(char *)sqlite3_column_text(stmt, 9);
            NSString *release_data=[NSString stringWithCString:release_data1 encoding:NSUTF8StringEncoding];
            //开始查找actors
            char *actors1=(char *)sqlite3_column_text(stmt, 10);
            NSString *actros=[NSString stringWithCString:actors1 encoding:NSUTF8StringEncoding];
            
            
            MovieDetaModel *model=[[MovieDetaModel alloc] init];
            model.plot_simple=plot_simple;
            model.tile=tile;
            model.genres=geners;
            model.country=country;
            model.runtime=runtime;
            model.poster=poster;
            model.rating_count=[NSNumber numberWithInt:[rating_count intValue]];
            model.rating=rating;
            model.release_date=release_data;
            model.actors=actros;
            [array addObject:model];
        }
    }else
    {
        NSLog(@"查找数据失败");
    }
    //释放游标
    sqlite3_finalize(stmt);
    
    return  array;
}
@end
